package com.yuntian.service.Impl;

import com.yuntian.dto.ProductShippingDTO;
import com.yuntian.entity.OrderData;
import com.yuntian.entity.OrderDetailData;
import com.yuntian.mapper.OrderDetailDataMapper;
import com.yuntian.mapper.OrderdataMapper;
import com.yuntian.service.OrderdataService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;


@Service
@RequiredArgsConstructor
public class OrderdataServiceImpl implements OrderdataService {



    private final OrderdataMapper orderMapper;
    private final OrderDetailDataMapper detailMapper;
    /**
     * 保存订单
     * @param order 待保存实体
     * @return 影响行数
     */

    @Transactional(rollbackFor = Exception.class)
    public void saveOrder(OrderData order, List<OrderDetailData> details) {
        // 1. 写主表
        orderMapper.insert(order);
        // 2. 写明细
        for (OrderDetailData d : details) {
            d.setT00201(order.getT00101()); // 把受注コード统一
            detailMapper.insertDetail(d);
        }
    }

    public List<ProductShippingDTO> getProductShippingList(LocalDate deliveryDate){
        return detailMapper.selectProductShippingList(deliveryDate);
    }
}
